<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>权限管理 | d2-crud-plus</title>
    <meta name="generator" content="VuePress 1.5.2">
    <link rel="icon" href="/d2-crud-plus/icon-only.png">
    <link rel="manifest" href="/d2-crud-plus/manifest.json">
    <link rel="apple-touch-icon" href="/d2-crud-plus/icons/apple-touch-icon-152x152.png">
    <link rel="mask-icon" href="/d2-crud-plus/icons/safari-pinned-tab.svg" color="#3eaf7c">
    <meta name="description" content="d2-crud-plus doc">
    <meta name="theme-color" content="#3eaf7c">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="msapplication-TileImage" content="/icons/msapplication-icon-144x144.png">
    <meta name="msapplication-TileColor" content="#000000">
    <link rel="preload" href="/d2-crud-plus/assets/css/0.styles.094573b2.css" as="style"><link rel="preload" href="/d2-crud-plus/assets/js/app.920fe806.js" as="script"><link rel="preload" href="/d2-crud-plus/assets/js/2.9f3ba775.js" as="script"><link rel="preload" href="/d2-crud-plus/assets/js/63.1ba8b586.js" as="script"><link rel="prefetch" href="/d2-crud-plus/assets/js/10.768312da.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/11.88f0302a.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/12.0a517745.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/13.a305030c.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/14.d7129909.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/15.1ff1eab0.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/16.b84cb74e.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/17.0e17fa97.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/18.b4667ec6.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/19.4a1d2603.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/20.e542b5eb.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/21.bea29276.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/22.5834f6ce.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/23.d514eada.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/24.10003849.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/25.238b33f6.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/26.64d5f744.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/27.20999cf2.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/28.a0ae569e.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/29.958d25f6.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/3.6fc0af51.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/30.5daeb87d.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/31.8f2a2581.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/32.bf1a9e00.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/33.fb5669a4.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/34.38bf074e.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/35.4789523c.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/36.5e0ded16.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/37.8b00f742.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/38.49c29967.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/39.f40a50c6.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/4.6d40d8fb.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/40.6422ce3c.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/41.76e6d756.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/42.2571374a.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/43.0e62c77f.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/44.59d7ad10.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/45.81bb2ffe.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/46.712a33f6.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/47.6a6f286c.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/48.25afab63.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/49.f6fd43e8.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/5.8571b144.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/50.5a933b10.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/51.fff19aba.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/52.5b7aa258.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/53.e207b0e8.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/54.c28e9900.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/55.23098b29.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/56.dd2936c0.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/57.e7938457.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/58.d206d759.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/59.066b3e6d.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/6.8660d829.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/60.f55b15a6.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/61.9c91c70f.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/62.bf4ab90c.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/64.53572d02.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/65.ba75d2b6.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/66.3b137930.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/67.3f157fd4.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/68.f1e28a8c.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/69.b8844926.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/7.d8550247.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/70.bb6afb7e.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/71.aa8ba6ae.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/8.b8c6f50a.js"><link rel="prefetch" href="/d2-crud-plus/assets/js/9.bb5c3b5b.js">
    <link rel="stylesheet" href="/d2-crud-plus/assets/css/0.styles.094573b2.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/d2-crud-plus/" class="home-link router-link-active"><!----> <span class="site-name">d2-crud-plus</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/d2-crud-plus/guide/" class="nav-link router-link-active">
  指南
</a></div><div class="nav-item"><a href="http://qiniu.veryreader.com/D2CrudPlusExample/index.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  演示
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <a href="https://github.com/greper/d2-crud-plus" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/d2-crud-plus/guide/" class="nav-link router-link-active">
  指南
</a></div><div class="nav-item"><a href="http://qiniu.veryreader.com/D2CrudPlusExample/index.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  演示
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <a href="https://github.com/greper/d2-crud-plus" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>指南</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/d2-crud-plus/guide/" class="sidebar-link">介绍</a></li><li><a href="/d2-crud-plus/guide/quickstart.html" class="sidebar-link">快速上手</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>进阶</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/d2-crud-plus/guide/mixins.html" class="sidebar-link">d2CrudPlus.crud</a></li><li><a href="/d2-crud-plus/guide/options.html" class="sidebar-link">crud配置项</a></li><li><a href="/d2-crud-plus/guide/structure.html" class="sidebar-link">数据结构</a></li><li><a href="/d2-crud-plus/guide/component.html" class="sidebar-link">组件配置</a></li><li><a href="/d2-crud-plus/guide/column-type.html" class="sidebar-link">字段类型</a></li><li><a href="/d2-crud-plus/guide/types.html" class="sidebar-link">字段类型列表</a></li><li><a href="/d2-crud-plus/guide/slot.html" class="sidebar-link">slot自定义</a></li><li><a href="/d2-crud-plus/guide/extends.html" class="sidebar-link">自定义扩展</a></li><li><a href="/d2-crud-plus/guide/dict.html" class="sidebar-link">数据字典</a></li><li><a href="/d2-crud-plus/guide/event.html" class="sidebar-link">事件监听</a></li><li><a href="/d2-crud-plus/guide/flat-data.html" class="sidebar-link">数据扁平化</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>附加功能</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/d2-crud-plus/guide/permission.html" class="active sidebar-link">权限管理</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/d2-crud-plus/guide/permission.html#_1-运行示例" class="sidebar-link">1. 运行示例</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/d2-crud-plus/guide/permission.html#_1-1-启动后端【java】" class="sidebar-link">1.1 启动后端【java】</a></li><li class="sidebar-sub-header"><a href="/d2-crud-plus/guide/permission.html#_1-2-启动d2-crud-plus-example" class="sidebar-link">1.2 启动d2-crud-plus-example</a></li><li class="sidebar-sub-header"><a href="/d2-crud-plus/guide/permission.html#_1-3-查看效果" class="sidebar-link">1.3 查看效果</a></li></ul></li><li class="sidebar-sub-header"><a href="/d2-crud-plus/guide/permission.html#_2-按钮权限" class="sidebar-link">2 按钮权限</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/d2-crud-plus/guide/permission.html#_2-1-directive控制" class="sidebar-link">2.1 directive控制</a></li><li class="sidebar-sub-header"><a href="/d2-crud-plus/guide/permission.html#_2-2-代码控制" class="sidebar-link">2.2 代码控制</a></li></ul></li><li class="sidebar-sub-header"><a href="/d2-crud-plus/guide/permission.html#_3-权限模块如何接入到你的d2-admin项目中" class="sidebar-link">3 权限模块如何接入到你的d2-admin项目中</a></li></ul></li><li><a href="/d2-crud-plus/guide/generate.html" class="sidebar-link">代码生成</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>更多帮助</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/d2-crud-plus/guide/upgrade.html" class="sidebar-link">版本升级</a></li><li><a href="/d2-crud-plus/guide/breakingchange.html" class="sidebar-link">版本变更说明</a></li><li><a href="/d2-crud-plus/guide/questions.html" class="sidebar-link">常见问题</a></li><li><a href="/d2-crud-plus/guide/contact.html" class="sidebar-link">联系作者</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>extends文档</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/d2-crud-plus/guide/extends/area-selector.html" class="sidebar-link">省市区选择组件</a></li><li><a href="/d2-crud-plus/guide/extends/avatar-cropper.html" class="sidebar-link">头像裁剪上传</a></li><li><a href="/d2-crud-plus/guide/extends/file-uploader.html" class="sidebar-link">文件上传组件</a></li><li><a href="/d2-crud-plus/guide/extends/full-editor.html" class="sidebar-link">富文本</a></li><li><a href="/d2-crud-plus/guide/extends/icon-selector.html" class="sidebar-link">icon选择组件</a></li><li><a href="/d2-crud-plus/guide/extends/tree-selector.html" class="sidebar-link">树形选择组件</a></li><li><a href="/d2-crud-plus/guide/extends/uploader.html" class="sidebar-link">文件上传lib</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>组件文档</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>ChangeLog</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="权限管理"><a href="#权限管理" class="header-anchor">#</a> 权限管理</h1> <p>RBAC权限管理模块</p> <h2 id="_1-运行示例"><a href="#_1-运行示例" class="header-anchor">#</a> 1. 运行示例</h2> <p>d2-crud-plus-example中提供权限管理模块，位于<code>/src/business/modules/permission</code>下</p> <h3 id="_1-1-启动后端【java】"><a href="#_1-1-启动后端【java】" class="header-anchor">#</a> 1.1 启动后端【java】</h3> <p><a href="https://github.com/greper/d2-crud-plus-server" target="_blank" rel="noopener noreferrer">d2-crud-plus-server<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p> <h4 id="_1-1-1-克隆项目"><a href="#_1-1-1-克隆项目" class="header-anchor">#</a> 1.1.1 克隆项目</h4> <div class="language-shell extra-class"><pre class="language-text"><code>git clone https://github.com/greper/d2-crud-plus-server.git
# 导入idea或eclipse
</code></pre></div><h4 id="_1-1-2-创建数据库"><a href="#_1-1-2-创建数据库" class="header-anchor">#</a> 1.1.2 创建数据库</h4> <div class="language- extra-class"><pre class="language-text"><code>创建数据库`d2p_pm`
server启动后会自动创建表
</code></pre></div><h4 id="_1-1-3修改数据库连接配置"><a href="#_1-1-3修改数据库连接配置" class="header-anchor">#</a> 1.1.3修改数据库连接配置</h4> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token comment"># ./api-service/src/main/resources/application.xml</span>
<span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">datasource</span><span class="token punctuation">:</span>
    <span class="token key atrule">username</span><span class="token punctuation">:</span> root
    <span class="token key atrule">password</span><span class="token punctuation">:</span> root
    <span class="token key atrule">url</span><span class="token punctuation">:</span> jdbc<span class="token punctuation">:</span>mysql<span class="token punctuation">:</span>//localhost<span class="token punctuation">:</span>3306/d2p_pm<span class="token punctuation">?</span>characterEncoding=utf8<span class="token important">&amp;zeroDateTimeBehavior</span>=convertToNull<span class="token important">&amp;useSSL</span>=false<span class="token important">&amp;serverTimezone</span>=Asia/Shanghai<span class="token important">&amp;allowMultiQueries</span>=true
</code></pre></div><h4 id="_1-1-4-启动api-server"><a href="#_1-1-4-启动api-server" class="header-anchor">#</a> 1.1.4 启动api-server</h4> <div class="language- extra-class"><pre class="language-text"><code>run ApiServerApplication
</code></pre></div><h3 id="_1-2-启动d2-crud-plus-example"><a href="#_1-2-启动d2-crud-plus-example" class="header-anchor">#</a> 1.2 启动d2-crud-plus-example</h3> <p>开启权限模块</p> <div class="language-shell extra-class"><pre class="language-text"><code>/packages/d2-crud-plus-example#  npm run dev:pm

# 或者
# 修改.env.development文件：VUE_APP_PM_ENABLED=true
/packages/d2-crud-plus-example#  npm run dev
</code></pre></div><h3 id="_1-3-查看效果"><a href="#_1-3-查看效果" class="header-anchor">#</a> 1.3 查看效果</h3> <ul><li>登录 admin/admin 可以查看全部菜单，拥有全部功能</li> <li>登录 test/123456 只能查看资源和角色管理菜单，无法看到用户管理菜单</li> <li>登录 readonly/123456 只能查看，不能编辑</li> <li>登录 authz/123456 只能查看与授权，不能编辑</li></ul> <p>系统默认数据不允许修改，如需体验，请自行添加数据进行测试</p> <h2 id="_2-按钮权限"><a href="#_2-按钮权限" class="header-anchor">#</a> 2 按钮权限</h2> <h3 id="_2-1-directive控制"><a href="#_2-1-directive控制" class="header-anchor">#</a> 2.1 directive控制</h3> <div class="language-html extra-class"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>el-button</span> <span class="token attr-name">v-permission</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span><span class="token punctuation">'</span>permission:role:add'<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>添加角色<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>el-button</span><span class="token punctuation">&gt;</span></span>
</code></pre></div><h3 id="_2-2-代码控制"><a href="#_2-2-代码控制" class="header-anchor">#</a> 2.2 代码控制</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
    <span class="token function">created</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">hasPermissions</span><span class="token punctuation">(</span><span class="token string">'permission:role:add'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;您拥有添加权限&quot;</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="_3-权限模块如何接入到你的d2-admin项目中"><a href="#_3-权限模块如何接入到你的d2-admin项目中" class="header-anchor">#</a> 3 权限模块如何接入到你的d2-admin项目中</h2> <ol><li>在<code>.env</code>中配置<code>VUE_APP_PM_ENABLED = true</code></li> <li>复制example中 <code>src/business/modules/permission</code> 到你的d2-admin项目中</li> <li>引入<code>permission</code>模块</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// src/business/modules/index.js 也可以在main.js中加入</span>
<span class="token keyword">import</span> <span class="token string">'@/business/modules/permission'</span> <span class="token comment">// 加载permission</span>
</code></pre></div><ol start="4"><li>复制<code>src/router/router.hook.js</code></li> <li>在<code>src/router/index.js</code> 的<code>beforeEach</code> 中加入以下代码</li></ol> <div class="language-js extra-class"><div class="highlight-lines"><br><br><br><br><br><br><br><br><br><br><br><div class="highlighted"> </div><div class="highlighted"> </div><div class="highlighted"> </div><div class="highlighted"> </div><div class="highlighted"> </div><div class="highlighted"> </div><br><br><br><br><br><br><br><br></div><pre class="language-js"><code>router<span class="token punctuation">.</span><span class="token function">beforeEach</span><span class="token punctuation">(</span><span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token parameter">to<span class="token punctuation">,</span> <span class="token keyword">from</span><span class="token punctuation">,</span> next</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token comment">// 确认已经加载多标签页数据 https://github.com/d2-projects/d2-admin/issues/201</span>
  <span class="token keyword">await</span> store<span class="token punctuation">.</span><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token string">'d2admin/page/isLoaded'</span><span class="token punctuation">)</span>
  <span class="token comment">// 确认已经加载组件尺寸设置 https://github.com/d2-projects/d2-admin/issues/198</span>
  <span class="token keyword">await</span> store<span class="token punctuation">.</span><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token string">'d2admin/size/isLoaded'</span><span class="token punctuation">)</span>
 <span class="token comment">// 进度条</span>
  NProgress<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token comment">// 关闭搜索面板</span>
  store<span class="token punctuation">.</span><span class="token function">commit</span><span class="token punctuation">(</span><span class="token string">'d2admin/search/set'</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">)</span>

  <span class="token comment">// 远程获取权限与菜单</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>RouterHook<span class="token punctuation">.</span>beforeEach<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> hookRet <span class="token operator">=</span> <span class="token keyword">await</span> RouterHook<span class="token punctuation">.</span><span class="token function">beforeEach</span><span class="token punctuation">(</span>to<span class="token punctuation">,</span> <span class="token keyword">from</span><span class="token punctuation">,</span> next<span class="token punctuation">)</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>hookRet<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
  <span class="token comment">// add end</span>

  <span class="token comment">// 验证当前路由所有的匹配中是否需要有登录验证的</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>to<span class="token punctuation">.</span>matched<span class="token punctuation">.</span><span class="token function">some</span><span class="token punctuation">(</span><span class="token parameter">r</span> <span class="token operator">=&gt;</span> r<span class="token punctuation">.</span>meta<span class="token punctuation">.</span>auth<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token operator">...</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><ol start="6"><li>在<code>/src/store/modules/d2admin/modules/account.js</code>中加入以下代码</li></ol> <div class="language-js extra-class"><div class="highlight-lines"><br><br><br><br><br><br><br><br><br><br><br><div class="highlighted"> </div><div class="highlighted"> </div><div class="highlighted"> </div><br><br><br><br><br><br></div><pre class="language-js"><code>    <span class="token function">logout</span> <span class="token punctuation">(</span><span class="token parameter"><span class="token punctuation">{</span> commit<span class="token punctuation">,</span> dispatch <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> confirm <span class="token operator">=</span> <span class="token boolean">false</span> <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token comment">/**
       * @description 注销
       */</span>
      <span class="token keyword">async</span> <span class="token keyword">function</span> <span class="token function">logout</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 删除cookie</span>
        util<span class="token punctuation">.</span>cookies<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token string">'token'</span><span class="token punctuation">)</span>
        util<span class="token punctuation">.</span>cookies<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token string">'uuid'</span><span class="token punctuation">)</span>
        <span class="token comment">// 清空 vuex 用户信息</span>
        <span class="token keyword">await</span> <span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token string">'d2admin/user/set'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> root<span class="token punctuation">:</span> <span class="token boolean">true</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>

        <span class="token comment">// 注销登录时清空permission信息</span>
        <span class="token function">commit</span><span class="token punctuation">(</span><span class="token string">'permission/clear'</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> root<span class="token punctuation">:</span> <span class="token boolean">true</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>

        <span class="token comment">// 跳转路由</span>
        router<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
          name<span class="token punctuation">:</span> <span class="token string">'login'</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span>
      <span class="token punctuation">}</span>
</code></pre></div></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/greper/d2-crud-plus/edit/master//d2-crud-plus-docs/guide/permission.md" target="_blank" rel="noopener noreferrer">在 GitHub 上编辑此页</a> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></div> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2020-6-27 1:21:32 PM</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/d2-crud-plus/guide/flat-data.html" class="prev">
        数据扁平化
      </a></span> <span class="next"><a href="/d2-crud-plus/guide/generate.html">
        代码生成
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"></div></div>
    <script src="/d2-crud-plus/assets/js/app.920fe806.js" defer></script><script src="/d2-crud-plus/assets/js/2.9f3ba775.js" defer></script><script src="/d2-crud-plus/assets/js/63.1ba8b586.js" defer></script>
  </body>
</html>
